/**
 * 
 */
package com.brother.goallife.rest.service.impl;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.brother.goallife.dto.Account;
import com.brother.goallife.exception.FatalException;
import com.brother.goallife.manager.UserManager;
import com.brother.goallife.rest.model.TestAccountDTO;
import com.brother.goallife.rest.service.UserModule;
import com.brother.goallife.type.AccountType;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @author anhtuan_truong
 * 
 */
@Path("/user")
@Api(value = "/user")
public class UserModuleImpl implements UserModule {

	private UserManager userManager;

	@GET
	@Path("/get")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Get user by username", response = TestAccountDTO.class)
	public Response getUserByUsername(
			@QueryParam("username") @ApiParam(value = "Username of user", required = true) String username)
			throws FatalException {

		Account account = userManager.findAccountByUsername(username);

		return Response.status(200).type(MediaType.APPLICATION_JSON)
				.entity(testAccountDTO(account)).build();
	}

	/*@GET
	@Path("/{userID}")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Get user by ID", response = TestAccountDTO.class)*/
	public Response getUserById(@PathParam(value="userID") @ApiParam(value = "ID of user", required = true) int id) throws FatalException {
		Account account = userManager.findAccoutByID(id);
		return Response.status(200).type(MediaType.APPLICATION_JSON)
				.entity(testAccountDTO(account)).build();
	}

	@POST
	@Path("/")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "update user", response = TestAccountDTO.class)
	public Response setUser(
			@FormParam("userID") @ApiParam(value = "ID of user", required = true) int id,
			@FormParam("accountType") @ApiParam(value = "New email to set", required = true) String accountType) throws FatalException {
		Account account = userManager.findAccoutByID(id);
		if (account == null) {
			// TODO: throws exception
		} else {
			account.setAccountType(AccountType.valueOf(accountType));
			userManager.updateAccount(account);
			
			account = userManager.findAccoutByID(id);
			return Response.status(200).type(MediaType.APPLICATION_JSON)
					.entity(testAccountDTO(account)).build();
			
		}
		
		return null;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	private TestAccountDTO testAccountDTO(Account account) {
		TestAccountDTO result = new TestAccountDTO();
		result.setMemberId(account.getMemberId());
		result.setUsername(account.getUsername());
		result.setPassword(account.getPassword());
		result.setAccountType(account.getAccountType().getCode());
		return result;
	}
}
